package cn.myfirefly.core.aop;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import cn.myfirefly.core.model.BaseFilterDto;
import cn.myfirefly.utils.FilterUtil;

/**
 * BjuiDataGrid参数处理
 * @author zhouxiaoming
 *
 */
@Aspect
@Component
public class BjuiDataGridFilterDtoAspect {
	private static Logger logger = LoggerFactory.getLogger(BjuiDataGridFilterDtoAspect.class);
	
	@Pointcut("@annotation(cn.myfirefly.core.annotation.BjuiDataGridFilterDtoWrap)")
    public void controllerAspect() {}
	
	@Before("controllerAspect()")
	public void before(JoinPoint joinPoint) {
		Object[] args = joinPoint.getArgs();
		if (args != null) {
			logger.info("start wrap FilterDto...");
			for (int i = 0; i < args.length; i++) {
				if (args[i] instanceof BaseFilterDto) {
					HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
					FilterUtil.wrapFilterDto((BaseFilterDto)args[i], request);
				}
			}
		}
	}
}
